package com.sunfeng.springcloud.config;

import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Objects;

/**
 * 限流器实现 主要用于设置限流请求的key   我们可以实现接口来指定需要对当前请求的那些因素进行流量控制
 *
 * 这个例子中我们对HostAddress
 *
 * @author sunfeng
 * @date 2021/04/07 17:08
 **/
@Service
public class IpAddressKeyResolver implements KeyResolver {
    @Override
    public Mono<String> resolve(ServerWebExchange exchange) {
        return Mono.just(Objects.requireNonNull(exchange.getRequest().getRemoteAddress()).getAddress().getHostAddress());
    }
}
